
#ifndef __QMC63XX_H__
#define __QMC63XX_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "esp_system.h"

#include <errno.h>
#include "esp_err.h"
#include "esp_log.h"

//芯片型号
//#define QMC63XX_CHIP_QMC6308		1
#define QMC63XX_CHIP_QMC6309		1
//#define QMC63XX_CHIP_QMC6310	1


//控制寄存器1 Control Registers1(0x0A)
//MODE<1:0>
typedef enum {
    MODE_SUSPEND=0,		//Suspend Mode 		待机模式
    MODE_NORMAL,		//Normal Mode  		正常模式
    MODE_SINGLE,		//Single Mode  		单次模式
    MODE_CONTINUOUS,	//Continuous Mode   连续模式
}SensorMode;
//控制寄存器2 Control Registers2(0x0B)
//SET/RESET MODE<1:0>
typedef enum  {
    SET_RESET_ON=0,
    SET_ONLY_ON,
    SET_RESET_OFF_EMPTY, //为了空闲保持兼容  QMC6309无意义 其它型号的和0x03的功能相同    
    SET_RESET_OFF,
}SetResetMode;

#if (QMC63XX_CHIP_QMC6309|QMC63XX_CHIP_QMC6310)
//ODR(Output data rate)
typedef enum {
#if QMC63XX_CHIP_QMC6309
//控制寄存器2 Control Registers2(0x0B)	
//ODR[2:0]
    DATARATE_1HZ=0,	
    DATARATE_10HZ,
    DATARATE_50HZ,
    DATARATE_100HZ,
    DATARATE_200HZ,
#elif QMC63XX_CHIP_QMC6310
//控制寄存器2 Control Registers1(0x0A)
//ODR<1:0
    DATARATE_10HZ=0,
    DATARATE_50HZ,
    DATARATE_100HZ,
    DATARATE_200HZ,
#endif
}OutputRate;
//OSR1(Over sample Rate)
typedef enum {
    OSR_8=0,
    OSR_4,
    OSR_2,
    OSR_1,
}OverSampleRatio;
//DSR(Down sample Rate OSR2)
typedef enum  {
	DSR_1=0,
	DSR_2, 
	DSR_4, 
	DSR_8,
}DownSampleRatio;
// Unit:Guass
typedef enum {
//控制寄存器2 Control Registers2(0x0B)	
//RNG[1:0]
    RANGE_30G=0,
    RANGE_12G,
    RANGE_8G,
    RANGE_2G,
}MagRange;
 #endif
 
typedef struct {
	float x;  
	float y; 
	float z;	
} QMC63XX_SensorData;
 
esp_err_t qmc63xx_init(void);
void qmc63xx_example(void);

// get the sensor chip id 
uint8_t qmc63xx_getChipID(void);
// get the sensor status
uint8_t qmc63xx_getStatus(void);
// get new data is ready?
bool qmc63xx_isDataReady(void);
// get data overflow occurs?
bool qmc63xx_isDataOverflow(void);
// Set the sensor mode
esp_err_t qmc63xx_setMode(SensorMode mode);
// Set the sensor Set/Reset Mode
esp_err_t qmc63xx_setResetMode(SetResetMode mode);

#if (QMC63XX_CHIP_QMC6309|QMC63XX_CHIP_QMC6310)
// Set the sensor ODR(Output data rate) 
esp_err_t qmc63xx_setDataOutputRate(OutputRate odr);
//Set the sensor OSR1(Over sample Rate)
esp_err_t qmc63xx_setOverSampleRate(OverSampleRatio osr);
//Set the sensor DSR(Down sample Rate OSR2)
esp_err_t qmc63xx_setDownSampleRate(DownSampleRatio dsr);
//Set the sensor Full Scale Range(Guass)
esp_err_t qmc63xx_setMagRange(MagRange range);

void qmc63xx_configMagnetometer(SensorMode mode, MagRange range, OutputRate odr,OverSampleRatio osr, DownSampleRatio dsr);           
#endif
// get the sensor X,Y and Z-axis magnetometer data readout 
esp_err_t qmc63xx_read_mag_xyz(QMC63XX_SensorData *sensordata);

#ifdef __cplusplus
}
#endif
#endif /* __QMC63XX_H__ */
